package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.topboya.linker.R;
import com.topboya.linker.model.response.VisitTypeResponse;
import com.topboya.linker.ui.adapter.VisitypeAdapter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.VisiTypePresenter;
import com.topboya.linker.ui.view.VisiTypeView;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;


public class VisiTypeActivity extends BaseActivity<VisiTypeView, VisiTypePresenter> implements VisiTypeView  {

    private static final String TAG = "VisiTypeActivity:";
    private RecyclerView rl_visit_tepe;
    private Button btn_select;
    private TextView toolbar_title;
    @Override
    protected int getLayoutId() {

        return R.layout.activity_visit_tpye;

    }

    @Override
    protected void initView() {

        rl_visit_tepe = findViewById(R.id.rl_visit_type);
        btn_select = findViewById(R.id.btn_select);
        toolbar_title = findViewById(R.id.toolbar_title);
        toolbar_title.setText("请选择任务类型");
    }

    @Override
    protected void initData() {

          mPresenter.getVisitType();
    }

    @Override
    protected void initListener() {


    }

    @Override
    protected VisiTypePresenter createPresenter() {

        return new VisiTypePresenter(this);
    }


    @Override
    public void getVisitType(List<VisitTypeResponse.Data> visitTypeResponses) {

        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 2);
        rl_visit_tepe.setLayoutManager(layoutManager);
        VisitypeAdapter visitypeAdapter = new VisitypeAdapter(this,(ArrayList<VisitTypeResponse.Data>) visitTypeResponses);
        rl_visit_tepe.setAdapter(visitypeAdapter);
        visitypeAdapter.setOnItemClickListener((v, position, count) -> {

            btn_select.setText("选择：（"+count+"）");

        });

        btn_select.setOnClickListener(v -> setlctvisitypevalues(visitypeAdapter));


    }

    private void setlctvisitypevalues(VisitypeAdapter visitypeAdapter) {

        ArrayList<VisitTypeResponse.Data> list =visitypeAdapter.getSelectedItem();

        backTostartActivity(list);


    }

    private void backTostartActivity(ArrayList<VisitTypeResponse.Data> list) {

        //数据是使用Intent返回
        Intent intent = new Intent();
        //把返回数据存入Intent
        intent.putExtra("result", list);
        //设置返回数据
        setResult(1001, intent);
        //关闭Activity
        finish();
    }
}
